package com.todod.service;

import java.time.LocalDateTime;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.todod.common.util.SnowflakeIdGenerator;
import com.todod.dto.ShuoMingFileDto;
import com.todod.entity.QueryProjectInfo;
import com.todod.mapper.ShuoMingFileMapper;
import com.todod.model.ShuoMingFile;

/**
 * @author lql
 * @date 2021年3月15日
 */
@Service
public class ShuoMingFileService {
	@Autowired
	private ShuoMingFileMapper shuoMingFileMapper;

	/**
	 * 
	 * @param _userId
	 * @param queryProjectInfo
	 * @return
	 */
	public IPage<ShuoMingFileDto> getListPage(Long _userId, QueryProjectInfo queryProjectInfo) {
		Page<ShuoMingFileDto> page = new Page<>(queryProjectInfo.getPage(), queryProjectInfo.getLimit());

		IPage<ShuoMingFileDto> iPage = shuoMingFileMapper.getListPage(page, _userId, queryProjectInfo);

		return iPage;
	}

	/**
	 * 
	 * @param userId
	 * @param shuoMingFile
	 */
	public void save(Long userId, ShuoMingFile shuoMingFile) {

		shuoMingFile.setId(SnowflakeIdGenerator.getId());
		shuoMingFile.setCode(String.valueOf(SnowflakeIdGenerator.getId()));
		shuoMingFile.setVersion(1);

		shuoMingFile.setIsNew(1l);
//		设置删除标识为1（默认：为1）
		shuoMingFile.setFlag(1);
		shuoMingFile.setCreatetimeTi(LocalDateTime.now());
		shuoMingFile.setCreateuserId(userId);
		shuoMingFileMapper.insert(shuoMingFile);
	}

	/**
	 * 
	 * @param _userId
	 * @param shuoMingFile
	 */
	public void edit(Long _userId, ShuoMingFile shuoMingFile) {

		ShuoMingFile shuoMingFile1 = shuoMingFileMapper.selectById(shuoMingFile.getId());
		shuoMingFile1.setIsNew(2l);

		shuoMingFile1.setCreateuserId(shuoMingFile1.getCreateuserId());
		shuoMingFile1.setCreatetimeTi(shuoMingFile1.getCreatetimeTi());
		shuoMingFile1.setModifyuserId(_userId);
		shuoMingFile1.setModifytimeTi(LocalDateTime.now());
		shuoMingFileMapper.updateById(shuoMingFile1);

		shuoMingFile.setId(SnowflakeIdGenerator.getId());
		shuoMingFile.setVersion(shuoMingFile1.getVersion() + 1);
		shuoMingFile.setCode(shuoMingFile1.getCode());
//		设置删除标识为1（默认：为1）
		shuoMingFile.setFlag(1);
		shuoMingFile.setIsNew(1l);

		shuoMingFile.setCreateuserId(_userId);
		shuoMingFile.setCreatetimeTi(LocalDateTime.now());
		shuoMingFileMapper.insert(shuoMingFile);
	}

	/**
	 * 
	 * @param companyId
	 * @param xiangmuId
	 * @return
	 */
	public List<ShuoMingFile> getListByFlag(Long companyId, Long xiangmuId) {
		QueryWrapper<ShuoMingFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dd_xm_id", xiangmuId);
		queryWrapper.eq("dd_department_id", companyId);
		queryWrapper.eq("dd_is_new", 1L);
		queryWrapper.orderByDesc("dd_version");

		List<ShuoMingFile> list = shuoMingFileMapper.selectList(queryWrapper);

		return list;
	}

	public List<ShuoMingFileDto> getListByCode(String code) {

		List<ShuoMingFileDto> list = shuoMingFileMapper.getListByCode(code);

		return list;
	}

	public List<ShuoMingFile> getListByXftId(Long code) {
		QueryWrapper<ShuoMingFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dd_classify_id", code);
		queryWrapper.orderByDesc("dd_sort_num");

		List<ShuoMingFile> list = shuoMingFileMapper.selectList(queryWrapper);

		return list;
	}

	/**
	 * 
	 * @param classifyId
	 * @return
	 */
	public List<ShuoMingFile> getListByClassifyId(Long classifyId) {
		QueryWrapper<ShuoMingFile> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("dd_classify_id", classifyId);
		queryWrapper.eq("dd_is_new", 1L);
		queryWrapper.eq("dd_flag", 1L);
		queryWrapper.orderByDesc("dd_sort_num");

		List<ShuoMingFile> list = shuoMingFileMapper.selectList(queryWrapper);

		return list;
	}

}
